package com.breaktian.assemble.workmanager;

import android.arch.lifecycle.LiveData;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import androidx.work.Constraints;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkContinuation;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.Worker;

/**
 * https://www.jianshu.com/p/2938f52a412e
 *
 */
public class WorkManagerUtil {

    /**
     * 启动一次性任务
     * @param worker
     *
     * @return uuid
     * */
    public static UUID startOneTimeWork(Worker worker){
        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(worker.getClass()).build();
        WorkManager.getInstance().enqueue(oneTimeWorkRequest);
        return oneTimeWorkRequest.getId();
    }

    /**
     * 启动一次性任务
     * @param worker
     * @param constraints
     *
     * @return uuid
     * */
    public static UUID startOneTimeWork(Worker worker, Constraints constraints){
        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest
                .Builder(worker.getClass())
                .setConstraints(constraints)
                .build();
        WorkManager.getInstance().enqueue(oneTimeWorkRequest);
        return oneTimeWorkRequest.getId();
    }

    /**
     * 启动周期性任务
     * @param worker
     * @param repeatInterval
     * @param timeUnit
     *
     * @return id
     * */
    public static UUID startPeriodWork(Worker worker,long repeatInterval,TimeUnit timeUnit){
        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest
                .Builder(worker.getClass(), repeatInterval, timeUnit)
                .build();
        WorkManager.getInstance().enqueue(periodicWorkRequest);
        return periodicWorkRequest.getId();
    }

    /**
     * 启动周期性任务
     * @param worker
     * @param repeatInterval
     * @param timeUnit
     * @param constraints 约束
     *
     * @return id
     * */
    public static UUID startPeriodWork(Worker worker,long repeatInterval,TimeUnit timeUnit, Constraints constraints){
        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest
                .Builder(worker.getClass(), repeatInterval, timeUnit)
                .setConstraints(constraints)
                .build();
        WorkManager.getInstance().enqueue(periodicWorkRequest);
        return periodicWorkRequest.getId();
    }

    /**
     * 任务链
     * */
    public static class WorkChain{
        WorkContinuation workContinuation;
        public WorkChain(){}

        public void beginWith(Worker worker){
            workContinuation = WorkManager.getInstance().beginWith(getOnetimeWork(worker));
        }

        public void then(Worker worker){
            if(workContinuation!=null){
                workContinuation.then(getOnetimeWork(worker));
            }
        }

        public void enqueue(){
            if(workContinuation!=null){
                workContinuation.enqueue();
            }
        }
    }


    public static OneTimeWorkRequest getOnetimeWork(Worker worker){
        return new OneTimeWorkRequest.Builder(worker.getClass()).build();
    }

    public static PeriodicWorkRequest getPeriodWork(Worker worker,long repeatInterval,TimeUnit timeUnit){
        return new PeriodicWorkRequest.Builder(worker.getClass(),repeatInterval,timeUnit).build();
    }


    public static WorkChain newWorkChain(){
        return new WorkChain();
    }



    /**
     * 根据id取消
     * @param id
     *
     * */
    public static void cancelById(UUID id){
        WorkManager.getInstance().cancelWorkById(id);
    }

    /**
     * 获取任务状态
     * @param id
     *
     * @return livedata
     * */
    public static LiveData<WorkInfo> getStatusById(UUID id){
        return WorkManager.getInstance().getWorkInfoByIdLiveData(id);
    }
}
